package com.pocketmenu.temp;

import java.util.Date;
import java.util.List;

import com.pocketmenu.business.Constants;
import com.pocketmenu.persistence.DatabaseDefaultValues;
import com.pocketmenu.persistence.Item;
import com.pocketmenu.persistence.ItemDataSource;
import com.pocketmenu.persistence.Order;
import com.pocketmenu.persistence.OrderDataSource;
import com.pocketmenu.persistence.OrderItemDataSource;
import com.pocketmenu.persistence.Restaurant;
import com.pocketmenu.persistence.RestaurantDataSource;
import com.pocketmenu.ui.OrderActivity;
import com.pocketmenu.ui.PreferencesActivity;
import com.pocketmenu.ui.R;

import com.pocketmenu.ui.RestaurantActivity;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;


public class TestDatabaseActivity extends ListActivity {
	  private RestaurantDataSource datasource;

	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_test_database);

	    datasource = new RestaurantDataSource(this);
	    try{
	    	datasource.open();	
	    	DatabaseDefaultValues df = new DatabaseDefaultValues(this);
	    	df.loadDefaulValues();	    	
	    }
	    catch (Exception e)
	    {
	    	System.out.print(e.toString());
	    }
	    
	    List<Restaurant> values = datasource.getAll();

	    // Use the SimpleCursorAdapter to show the
	    // elements in a ListView
	    ArrayAdapter<Restaurant> adapter = new ArrayAdapter<Restaurant>(this,
	        android.R.layout.simple_list_item_1, values);
	    setListAdapter(adapter);
	  }

	  // Will be called via the onClick attribute
	  // of the buttons in main.xml
	  public void onClick(View view) {
	    @SuppressWarnings("unchecked")
	    ArrayAdapter<Restaurant> adapter = (ArrayAdapter<Restaurant>) getListAdapter();
	    Restaurant restaurant = null;
	    switch (view.getId()) {
	    case R.id.add:
	      
	      
	      // Save the new comment to the database
	      //restaurant = datasource.create("Buena comida", "Muy buena comida aqui", "123456", "Direccion de buena comida", "314145151");
	      adapter.add(restaurant);
	      break;
	    case R.id.delete:
	      if (getListAdapter().getCount() > 0) {
	    	restaurant = (Restaurant) getListAdapter().getItem(0);
	        datasource.delete(restaurant);
	        adapter.remove(restaurant);
	      }
	      break;
	    }
	    adapter.notifyDataSetChanged();
	  }
	  
	  
	  public void irRestaurant(View v){
		  Intent intent = new Intent(getApplicationContext(), RestaurantActivity.class);
		  startActivity(intent);
	  }
	  
	  public void irPreferencias(View v){
		  Intent intent = new Intent(getApplicationContext(), PreferencesActivity.class);
		  startActivity(intent);
	  }
	  
	  public void bOrder_Click(View view)
	  {
		  insertTestOrder();
		  Intent intent = new Intent(getApplicationContext(), OrderActivity.class);
		  intent.putExtra(Constants.EXTRA_ORDER_ID, 1);
		  intent.putExtra(Constants.EXTRA_RESTAURANT_ID, 1);
		  startActivity(intent);
	  }

	  public void insertTestOrder()
	  {				  
		  OrderItemDataSource orderItemDataSource = new OrderItemDataSource(this);
		  OrderDataSource orderDatasource = new OrderDataSource(this);
		  ItemDataSource itemDatasource = new ItemDataSource(this);
		  
		  orderDatasource.open();
		  orderItemDataSource.open();
		  itemDatasource.open();
		  
		  Date now = new Date();		  
		  Order newOrder = orderDatasource.create(0, false, "", "", now);
		  		  
		  /*
		  int total = 10000; 
		  orderItemDataSource.create(total * 5, 5, 1, newOrder.getId());		  		  		  
		  orderItemDataSource.create(total * 4, 4, 2, newOrder.getId());		 		  
		  orderItemDataSource.create(total * 3, 3, 3, newOrder.getId());
		  orderItemDataSource.create(total * 2, 2, 4, newOrder.getId());
		  orderItemDataSource.create(total, 1, 5, newOrder.getId());
		  */
		  orderItemDataSource.close();
		  orderDatasource.close();
	  }
	  
	  @Override
	  protected void onResume() {
//	    datasource.open();
	    super.onResume();
	  }

	  @Override
	  protected void onPause() {
	    datasource.close();
	    super.onPause();
	  }
	  
	  

	} 